using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DeliveryDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Con"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = String.Format("select * from Delivery where DeliveryNo={0}", Request.QueryString["deliverynumber"]);
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        Label10.Text = dr.GetInt32(0).ToString();
        txtWeight.Text = dr.GetDouble(1).ToString();
        txtPost.Text = dr.GetDateTime(2).ToShortDateString();
        txtService.Text = dr.GetInt32(3).ToString();
        txtDest.Text = dr.GetString(4).ToString();
        txtCust.Text = dr.GetInt32(5).ToString();
        txtFeetype.Text = dr.GetInt32(6).ToString();
        if (dr.GetBoolean(7))
            cbxStatus.SelectedIndex = 1;
        else
            cbxStatus.SelectedIndex = 0;
        if (!dr.IsDBNull(8))
            txtReceive.Text = dr.GetDateTime(8).ToShortDateString();
        con.Close();

        // define range for input date
        rvDOP.Type = ValidationDataType.Date;
        rvDOP.MinimumValue = "1/1/1900";
        rvDOP.MaximumValue = DateTime.Now.ToShortDateString();
        rvReceive.Type = ValidationDataType.Date;
        rvReceive.MinimumValue = txtPost.Text;
        rvReceive.MaximumValue = DateTime.Now.ToShortDateString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Con"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "update Delivery set Weight=@wei, DateOfPosting=@DOP, ServiceID=@ser, "
                + "DestinationAddress=@dest, CustID=@cust, FeeTypeID=@fee, Status=@stt, ReceiveDate=@receive "
                + "where DeliveryNo=@no";
            cmd.Parameters.AddWithValue("wei", float.Parse(txtWeight.Text));
            cmd.Parameters.AddWithValue("DOP", txtPost.Text);
            cmd.Parameters.AddWithValue("ser", Int32.Parse(txtService.Text));
            cmd.Parameters.AddWithValue("dest", txtDest.Text.Trim());
            cmd.Parameters.AddWithValue("cust", Int32.Parse(txtCust.Text));
            cmd.Parameters.AddWithValue("fee", Int32.Parse(txtFeetype.Text));
            cmd.Parameters.AddWithValue("stt", cbxStatus.SelectedIndex);
            cmd.Parameters.AddWithValue("receive", txtReceive.Text);
            cmd.Parameters.AddWithValue("no", Int32.Parse(Label10.Text));
            cmd.ExecuteNonQuery();
            con.Close();

            Label11.ForeColor = System.Drawing.Color.Lime;
            Label11.Text = "Update successfully.";
        }
        catch (Exception ex)
        {
            Label11.ForeColor = System.Drawing.Color.Red;
            Label11.Text = ex.Message;
            return;
        }
    }
}
